package org.dromara.apartment.controller;

import java.util.List;

import com.alibaba.nacos.api.model.v2.Result;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.apartment.domain.RoomInfo;
import org.dromara.apartment.domain.bo.ApartmentInfoBo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.apartment.domain.vo.RoomInfoVo;
import org.dromara.apartment.domain.bo.RoomInfoBo;
import org.dromara.apartment.service.IRoomInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 房间信息
 * 前端访问路由地址为:/apartment/info
 *
 * @author ckj
 * @date 2025-06-24
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/room")
public class RoomInfoController extends BaseController {

    private final IRoomInfoService roomInfoService;

    /**
     * 查询房间信息列表
     */
    @SaCheckPermission("apartment:room:list")
    @GetMapping("/list")
    public TableDataInfo<RoomInfoVo> list(RoomInfoBo bo, PageQuery pageQuery) {
        return roomInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出房间信息列表
     */
    @SaCheckPermission("apartment:room:export")
    @Log(title = "房间信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(RoomInfoBo bo, HttpServletResponse response) {
        List<RoomInfoVo> list = roomInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "房间信息", RoomInfoVo.class, response);
    }

    /**
     * 获取房间信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("apartment:room:query")
    @GetMapping("/{id}")
    public R<RoomInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(roomInfoService.queryById(id));
    }

    /**
     * 新增房间信息
     */
    @SaCheckPermission("apartment:room:add")
    @Log(title = "房间信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody RoomInfoBo bo) {
        return toAjax(roomInfoService.insertByBo(bo));
    }

    /**
     * 修改房间信息
     */
    @SaCheckPermission("apartment:room:edit")
    @Log(title = "房间信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody RoomInfoBo bo) {
        return toAjax(roomInfoService.updateByBo(bo));
    }

    /**
     * 删除房间信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("apartment:room:remove")
    @Log(title = "房间信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(roomInfoService.deleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 恢复房间信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("apartment:room:recover")
    @Log(title = "恢复房间信息", businessType = BusinessType.DELETE)
    @PutMapping("recover/{ids}")
    public R<Void> recover(@NotEmpty(message = "主键不能为空")
                           @PathVariable("ids") Long[] ids) {
        return toAjax(roomInfoService.recoverWithValidByIds(List.of(ids), true));
    }


    /**
     * 永久删除房间信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("apartment:room:foreverDelete")
    @Log(title = "永久删除房间信息", businessType = BusinessType.DELETE)
    @DeleteMapping("foreverDelete/{ids}")
    public R<Void> foreverDelete(@NotEmpty(message = "主键不能为空")
                                 @PathVariable("ids") Long[] ids) {
        return toAjax(roomInfoService.foreverDeleteWithValidByIds(List.of(ids), true));
    }

    /**
     * 房间状态修改
     */
    @SaCheckPermission("apartment:room:edit")
    @Log(title = "房间状态修改", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R<Void> changeStatus(@RequestBody RoomInfoBo bo) {
        return toAjax(roomInfoService.updateStatus(bo));
    }
}
